<?php
class My_EventFollowerController extends MM_Controller_My_Action
{	
	/**
	 * @var MM_Service_Admin_EventGuests 活动嘉宾
	 */
	private $serviceEG = null;
	
	/**
	 * @var MM_Service_Admin_Guests 嘉宾
	 */
	private $serviceG = null;
	
	/**
	 * 初始化
	 */
	public function init()
	{
		parent::init();
		$this->serviceEG = new MM_Service_Admin_EventGuests();
		$this->serviceG	 = new MM_Service_Admin_Guests();
	}
	
	/**
	 * index
	 */
	public function indexAction()
	{
		$eid = $this->httpdata["eid"];
		/* $eventname = null;
		if($eid){
			$service =  new MM_Service_Admin_Event();
			$resultData = $service->fetchAll(null,null,"eid=$eid",null,true);
			if($resultData->getCode() ==1){
				$data = $resultData->getData();
				$eventname = $data['name'];
			}
		} */
		$this->pageview->assign("eventid",$eid);
		$this->helper->setPageView($this->pageview, WEB_TITLE, "eventfollower", null);
	}
	
	/**
	 * page
	 */
	public function pageAction()
	{
		if ( !empty($this->httpdata['act']) && isset($this->httpdata['act']) ) {
			$dt = new Mediad2_Utility_Datatable($this->serviceEG);
			if(!isset($this->httpdata['ovar'])) {
				$this->httpdata['ovar'] = "addtime";
			}
			$where = null;
			if(isset($this->httpdata['eid'])) {
				$where = "eid=".$this->httpdata['eid'];
			}
			$dt->init($this->httpdata['act'], array("name"),$where);
			$dt->create();
			$dt->response();
		}
	}
	
	/**
	 * create guests[新增嘉宾]
	 */
	public function createAction()
	{
		if ( ($this->uid<1)
				|| !isset($this->httpdata['name']) || empty($this->httpdata['name']) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/name');
		} else {
			//封装guestmodel
			$guests = new MM_Model_Guests();
			$guests->setName($this->httpdata['name']);
			$guests->setUid($this->uid);
			$guests->setAddtime(time());
			if (isset($this->httpdata['pinyin'])) $guests->setPinyin($this->httpdata['pinyin']);
			if (isset($this->httpdata['company'])) $guests->setCompany($this->httpdata['company']);
			if (isset($this->httpdata['position'])) $guests->setPosition($this->httpdata['position']);
			if (isset($this->httpdata['email'])) $guests->setEmail($this->httpdata['email']);
			if (isset($this->httpdata['qq'])) $guests->setQq($this->httpdata['qq']);
			if (isset($this->httpdata['mobile'])) $guests->setMobile($this->httpdata['mobile']);
			if (isset($this->httpdata['website'])) $guests->setWebsite($this->httpdata['website']);
			if (isset($this->httpdata['desc'])) $guests->setDesc($this->httpdata['desc']);
			if (isset($this->httpdata['sinaweibo'])) $guests->setSinaweibo($this->httpdata['sinaweibo']);
			if (isset($this->httpdata['sinanickname'])) $guests->setSinanickname($this->httpdata['sinanickname']);
			if (isset($this->httpdata['qqweibo'])) $guests->setQqweibo($this->httpdata['qqweibo']);
			if (isset($this->httpdata['qqnickname'])) $guests->setQqnickname($this->httpdata['qqnickname']);
			if (isset($this->httpdata['avatar'])) $guests->setAvatar($this->httpdata['avatar']);
	
			$this->result = $this->serviceG->insert($guests);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * update guests[更新嘉宾]
	 */
	public function updateAction()
	{
		if ( ($this->uid<1) || ($this->gid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/gid');
		} else {
			$this->helper->log("set guest det");
			$this->helper->log("pinyin".$this->httpdata['pinyin']);
			$this->result = $this->serviceG->updateGuests($this->gid, $this->httpdata);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * delete guests[删除嘉宾]
	 */
	public function delAction()
	{
		if ( ($this->uid<1) || ($this->gid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/gid');
		} else {
			$this->result = $this->serviceG->delGuests($this->uid, $this->gid);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * choose guests to event
	 */
	public function chooseAction()
	{
		if ( ($this->uid<1) || ($this->eid<1) || ($this->gid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/eid/gid');
		} else {
			$this->result = $this->serviceEG->chooseGuests($this->uid, $this->gid, $this->eid, $this->esid);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * remove guests from event
	 */
	public function removeAction()
	{
		if ( ($this->uid<1) || ($this->eid<1) || ($this->gid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/eid/gid');
		} else {
			$this->result = $this->serviceEG->removeGuests($this->uid, $this->gid, $this->eid, $this->esid);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * get event content
	 */
	public function listeventguestsAction()
	{
		if ( ($this->uid<1) || ($this->eid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/eid');
		} else {
			$where = "eid = $this->eid";
			$order = "egid asc";
			$this->result = $this->serviceEG->fetchAll(null,null,$where,$order,true);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * get guests
	 */
	public function listAction()
	{
		if ( ($this->uid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid');
		} else {
			$where = "uid = $this->uid";
			$order = "gid asc";
			$this->result = $this->serviceG->fetchAll(null,null,$where,$order,true);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * get guest by gid (guest)
	 */
	public function getAction()
	{
		if ( ($this->uid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid');
		} else {
			$where = sprintf("gid = '%s'",$this->httpdata['gid']);
			$order = "gid asc";
			$this->result = $this->serviceG->fetchAll(null,null,$where,$order,true);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * get guests by gids (event_guest)
	 */
	public function getbyidAction()
	{
		if ( ($this->uid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid');
		} else {
			$where = sprintf("gid in (%s)",$this->httpdata['gids']);
			$order = "gid asc";
			$this->result = $this->serviceEG->fetchAll(null,null,$where,$order,true);
		}
		echo $this->encodeResult($this->result);
	}
}